/*
 * Application : Deploy Depot
 * Page : commonValidation.js
 * Purpose: 
 * Created Date : Feb 22, 2012
 * 
 */ 

var commonValidation = new function() {
	/**
	 * Check for XML success response
	 */
	this.isSuccess = function(xml){
		if(common.getCDATAValue(xml.getElementsByTagName("status")[0])=="success")
			return true;
		return false;
	};
	this.isInput = function(xml){
		if(common.getCDATAValue(xml.getElementsByTagName("status")[0])=="input")
			return true;
		return false;
	};
	/**
	 * Validating For Null Check.
	 */
	this.NullValidate = function(obj) {
		
		if(obj == null || obj == undefined || obj == "null" || obj == ""){
			return false;
		}
		return true;
		
	};
	/**
	 * Validating For Null Check on XML response; if found null return ''.
	 */
	this.XMLNullValidate = function(obj){
		return commonValidation.NullValidate(obj)?obj:'';
	};
	// Validating Combo Box.
	this.validateComboBox = function (comboBoxObj) {
		if(commonValidation.NullValidate(comboBoxObj)){
			var comboBoxValue = comboBoxObj.options[comboBoxObj.selectedIndex].value;
			comboBoxValue = common.trim(comboBoxValue);
			if(comboBoxValue == "Please Select" || comboBoxValue == "Select"){
				return true;
			}
		}
		return false;
	};
	
	
	// Validating For Empty TextBox Check.
	this.isEmpty = function (txtBoxObj) {
		if(commonValidation.NullValidate(txtBoxObj)){
			var txtVal = common.trim(txtBoxObj.value);
			if(txtVal=="" || ! (/\S+/).test(txtVal)){
				return true;
			}
		}
		return false;
	};
	
	this.isChecked = function (containerElementObj) {
		if(commonValidation.NullValidate(containerElementObj)){
			for(var k=0; k < containerElementObj.childNodes.length; k++) {
				var divElement = containerElementObj.childNodes[k];
				var which_Obj = divElement.childNodes[0];
				if(commonValidation.NullValidate(which_Obj) && which_Obj.type == "radio" && true == which_Obj.checked){
					return false;
				}else if(commonValidation.NullValidate(which_Obj) && which_Obj.type == "checkbox" && true == which_Obj.checked){
					return false;
		        }
			}         	
		}
		return true;
	};
	
	this.validateStoreId = function(storelistTxtBox){
		var arrStoreId = storelistTxtBox.value.split(",");
		var validformat=/^[RT]\d{3}$/; //Basic check for format validity
		for(i=0;i< arrStoreId.length;i++){
			var storeId = common.trim(arrStoreId[i]);
			if (!validformat.test(storeId)){
				return true;
			}
		}
		return false;
	};
	
	this.isDuplicate = function (storelistTxtBox){
		var arrStoreId = storelistTxtBox.value.split(",");
		var arrUniqueStoreId = commonValidation.eliminateDuplicates(arrStoreId);
		if(arrStoreId.length != arrUniqueStoreId.length){
			return true;
		}
		return false;
	};
	
	this.eliminateDuplicates = function (arr) {
		  var i,
		      len=arr.length,
		      out=[],
		      obj={};

		  for (i=0;i<len;i++) {
		    obj[common.trim(arr[i])]=0;
		  }
		  for (i in obj) {
		    out.push(i);
		  }
		  return out;
		};


	
	this.isValidDate = function (input) {
		var validformat=/^\d{2}\/\d{2}\/\d{2}$/; //Basic check for format validity
		var message = null;
		inputDate =  common.trim(input.value);
		if (!validformat.test(inputDate)){
			message = "Invalid Date Format.(mm/dd/yy)";
		}else{ //Detailed check for valid date ranges
			var monthfield=inputDate.split("/")[0];
			var dayfield=inputDate.split("/")[1];
			var yearfield=inputDate.split("/")[2];
			yearfield = parseInt(yearfield, 10);
		    yearfield = 2000 + yearfield;
			var dayobj = new Date(yearfield, monthfield-1, dayfield);
			if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
				message = "Invalid Day, Month, or Year range detected";
		}
		return message;
	};
	
};
